<html>
<head>
    <meta charset="utf-8"/>
<meta name="description" content=""/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>

<title>JS正则实现Json文本压缩去除空格效果 | 絷缘的博客</title>

<link rel="shortcut icon" href="https://zydnc.gitee.io/favicon.ico?v=1732378515642">

<link href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://zydnc.gitee.io/styles/main.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/css/bootstrap.min.css">

<script src="https://cdn.jsdelivr.net/npm/@highlightjs/cdn-assets/highlight.min.js"></script>
<script src="https://cdn.bootcss.com/highlight.js/9.15.10/languages/dockerfile.min.js"></script>
<script src="https://cdn.bootcss.com/highlight.js/9.15.10/languages/dart.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/moment@2.27.0/moment.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/js/bootstrap.min.js"></script>
<!-- DEMO JS -->
<!--<script src="media/scripts/index.js"></script>-->



    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.css">
 <script src='//unpkg.com/valine/dist/Valine.min.js'></script>
</head>
<body>
<div class="main gt-bg-theme-color-first">
    <nav class="navbar navbar-expand-lg">
    <div class="navbar-brand">
        <img class="user-avatar" src="/images/avatar.png" alt="头像">
        <div class="site-name gt-c-content-color-first">
            絷缘的博客
        </div>
    </div>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <i class="fas fa-bars gt-c-content-color-first" style="font-size: 18px"></i>
    </button>
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <div class="navbar-nav mr-auto" style="text-align: center">
            
                <div class="nav-item">
                    
                        <a href="/" class="menu gt-a-link">
                            首页
                        </a>
                    
                </div>
            
                <div class="nav-item">
                    
                        <a href="/archives" class="menu gt-a-link">
                            归档
                        </a>
                    
                </div>
            
                <div class="nav-item">
                    
                        <a href="/tags" class="menu gt-a-link">
                            标签
                        </a>
                    
                </div>
            
                <div class="nav-item">
                    
                        <a href="/about" class="menu gt-a-link">
                            关于
                        </a>
                    
                </div>
            
                <div class="nav-item">
                    
                        <a href="/friends" class="menu gt-a-link">
                            友链
                        </a>
                    
                </div>
            
        </div>
        <div style="text-align: center">
            <form id="gridea-search-form" style="position: relative" data-update="1732378515642" action="/search/index.html">
                <input class="search-input" autocomplete="off" spellcheck="false" name="q" placeholder="搜索文章" />
                <i class="fas fa-search gt-c-content-color-first" style="position: absolute; top: 9px; left: 10px;"></i>
            </form>
        </div>
    </div>
</nav>

    <div class="post-container">
        <div class="post-detail gt-bg-theme-color-second">
            <article class="gt-post-content">
                <h2 class="post-title">
                    JS正则实现Json文本压缩去除空格效果
                </h2>
                <div class="post-info">
                    <time class="post-time gt-c-content-color-first">
                        · 2021-01-22 ·
                    </time>
                    
                        <a href="https://zydnc.gitee.io/js/" class="post-tags">
                            # JS
                        </a>
                    
                        <a href="https://zydnc.gitee.io/NQ4hTFbHKX/" class="post-tags">
                            # Regex
                        </a>
                    
                        <a href="https://zydnc.gitee.io/RTDPTZadb2/" class="post-tags">
                            # 正则表达式
                        </a>
                    
                </div>
                <div class="post-content">
                    <h2 id="一-准备一段测试用的json文本">一、准备一段测试用的Json文本</h2>
<pre><code class="language-json">{
    &quot;age&quot; : 12,
    &quot;name&quot; : &quot;zhiyuan&quot;,
    &quot;gender&quot; : &quot;male&quot;,
    &quot;skill&quot; : [&quot;J a v a&quot;,&quot;C&quot;,&quot;J S&quot;,&quot;Py th on&quot;]
}
</code></pre>
<h2 id="二-去除除value值中的空白字符之外的所有空白字符">二、去除除value值中的空白字符之外的所有空白字符</h2>
<pre><code class="language-js">function clearEmpty(){
    var json = document.getElementById(&quot;content&quot;).value;
    console.log(json);
    let str = json.replace(&quot;\r\n&quot;,&quot;&quot;);
    let newStr = str.trim().replace(/\s*\[\s*/g,&quot;\[&quot;).replace(/\s*\]\s*/g,&quot;\]&quot;).replace(/\s*\{\s*/g,&quot;\{&quot;).replace(/\s*\}\s*/g,&quot;\}&quot;).replace(/&quot;\s*:\s*&quot;/g,'&quot;:&quot;').replace(/&quot;\s*:\s*/g,'&quot;:').replace(/&quot;\s*,\s*/g,'&quot;,').replace(/,\s*&quot;/g,',&quot;').replace(/]\s*,\s*/g,&quot;],&quot;);
    console.log(newStr);
    document.getElementById(&quot;new-content&quot;).innerText = newStr;
}
</code></pre>
<h2 id="三-写一个难看的页面">三、写一个难看的页面</h2>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;title&gt;Json文本格式压缩&lt;/title&gt;
&lt;/head&gt;
&lt;style&gt;
    *{
        margin: 0;
        padding: 0;
        box-sizing: border-box;
        font-family:微软雅黑,sans-serif;
    }
    .container{
        width: 100%;
        height:auto;
        background-color:#f8f8f8;
        padding:20px;
        position: relative;
    }
    #content,#new-content{
        width: 100%;
        height:300px;
        background-color: #ffffff;
        color:#666;
        font-size:14px;
        outline:none;
        border:1px solid #1DB69A;
        border-radius:10px;
        padding:20px;
    }
    button{
        background-color:#FFFFFF;
        color: #1DB69A;
        height: 50px;
        width: 150px;
        font-size: 18px;
        outline:none;
        border:1px solid #1DB69A;
        cursor: pointer;
        border-radius:50px;
        margin:20px;
        position: absolute;
        right:20px;
        top:250px;
    }
    button:hover{
        background-color:#1DB69A;
        color: #FFFFFF;
    }
    button:focus{
        background-color:#009688;
        color:#FFFFFF;
    }
    .line{
        width:980px;
        margin:20px auto;
        font-size:20px;
        font-weight:bold;
        border-left:300px solid #1DB69A;
        border-right:300px solid #1DB69A;
        line-height: 2px;
        text-align: center;
        color: #1DB69A;
    }
&lt;/style&gt;
&lt;body&gt;
    &lt;div class=&quot;container&quot;&gt;
        &lt;div class=&quot;line&quot;&gt;请将json格式文本粘贴到文本框中&lt;/div&gt;
        &lt;textarea name=&quot;content&quot; cols=&quot;50&quot; rows=&quot;20&quot; id=&quot;content&quot;&gt;&lt;/textarea&gt;
        &lt;button onclick=&quot;clearEmpty()&quot;&gt;去除所有空格&lt;/button&gt;
        &lt;div class=&quot;line&quot;&gt;下面将生成去除空格的Json文本&lt;/div&gt;
        &lt;textarea name=&quot;new-content&quot; id=&quot;new-content&quot; cols=&quot;50&quot; rows=&quot;20&quot; disabled&gt;&lt;/textarea&gt;
    &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<h2 id="四-测试一下效果">四、测试一下效果</h2>
<figure data-type="image" tabindex="1"><img src="https://zydnc.gitee.io/post-images/1611323854003.png" alt="" loading="lazy"></figure>
<blockquote>
<p>原文作者：絷缘<br>
作者邮箱：zhiyuanworkemail@163.com<br>
原文地址：<a href="https://zhiyuandnc.github.io/ZeuH4X3iy/">https://zhiyuandnc.github.io/ZeuH4X3iy/</a><br>
版权声明：本文为博主原创文章，转载请注明原文链接作者信息</p>
</blockquote>

                </div>
            </article>
        </div>

        
            <div class="next-post">
                <div class="next gt-c-content-color-first">下一篇</div>
                <a href="https://zydnc.gitee.io/ORutvrQMw/" class="post-title gt-a-link">
                    CSS3新增选择器记录
                </a>
            </div>
        

        

        

        

        <div class="site-footer gt-c-content-color-first">
    <div class="slogan gt-c-content-color-first">事实不以人的意志为转移</div>
    <div class="social-container">
        
            
                <a href="#" target="_blank">
                    <i class="fab fa-github gt-c-content-color-first"></i>
                </a>
            
        
            
                <a href="#" target="_blank">
                    <i class="fab fa-telegram gt-c-content-color-first"></i>
                </a>
            
        
            
                <a href="#" target="_blank">
                    <i class="fab fa-qq gt-c-content-color-first"></i>
                </a>
            
        
            
                <a href="#" target="_blank">
                    <i class="fab fa-bilibili gt-c-content-color-first"></i>
                </a>
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
    </div>
    <div class="footer-info">
        Copyright&ensp;&copy;&ensp;絷缘的博客<br/><a href="https://beian.miit.gov.cn/" target="_blank">晋ICP备20001723号-1</a>
    </div>
    <div>
        Theme by <a href="https://imhanjie.com/" target="_blank">imhanjie</a>, Powered by <a
                href="https://github.com/getgridea/gridea" target="_blank">Gridea | <a href="https://zydnc.gitee.io/atom.xml" target="_blank">RSS</a></a>
    </div>
</div>

<script>
  hljs.initHighlightingOnLoad()
</script>

    </div>
</div>
</body>
</html>
